package com.zc.manager.network.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zc.component.network.core.NetworkManager;
import com.zc.component.network.core.NetworkProvider;
import com.zc.manager.network.domain.IotNetworkConfig;
import com.zc.manager.network.response.NetworkTypeInfo;
import com.zc.manager.network.service.IotNetworkConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 网络组件Controller
 *
 * @author wangjiabao
 * @date 2022-10-19
 */
@RestController
@RequestMapping("/iot/network")
public class IotNetworkConfigController extends BaseController {

    @Resource
    private IotNetworkConfigService iotNetworkConfigService;

    @Resource
    private NetworkManager networkManager;

    /**
     * 查询网络组件列表
     */
    @PreAuthorize("@ss.hasPermi('iot:network:list')")
    @CrossOrigin
    @GetMapping("/list")
    public TableDataInfo list(IotNetworkConfig iotNetworkConfig) {
        startPage();
        List<IotNetworkConfig> list = iotNetworkConfigService.selectIotNetworkConfigList(iotNetworkConfig);
        return getDataTable(list);
    }

    /**
     * 无分页查询网络组件列表
     */
    @PreAuthorize("@ss.hasPermi('iot:network:list')")
    @CrossOrigin
    @GetMapping("/query")
    public AjaxResult query(IotNetworkConfig iotNetworkConfig) {
        List<IotNetworkConfig> list = iotNetworkConfigService.selectIotNetworkConfigList(iotNetworkConfig);
        return AjaxResult.success(list);
    }

    /**
     * 新增组件
     */
    @PreAuthorize("@ss.hasPermi('iot:network:add')")
    @Log(title = "组件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody IotNetworkConfig iotNetworkConfig) {
        return toAjax(iotNetworkConfigService.insertIotNetworkConfig(iotNetworkConfig));
    }

    /**
     * 获取组件详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:network:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iotNetworkConfigService.selectIotNetworkConfigById(id));
    }

    /**
     * 修改组件
     */
    @PreAuthorize("@ss.hasPermi('iot:network:edit')")
    @Log(title = "组件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody IotNetworkConfig iotNetworkConfig) {
        return toAjax(iotNetworkConfigService.updateIotNetworkConfig(iotNetworkConfig));
    }

    /**
     * 启动组件
     */
    @PreAuthorize("@ss.hasPermi('iot:network:edit')")
    @Log(title = "启动组件", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/startup/{id}")
    public AjaxResult startupNetwork(@PathVariable("id") Long id) {
        return toAjax(iotNetworkConfigService.startupNetworkById(id));
    }

    /**
     * 停止组件
     */
    @PreAuthorize("@ss.hasPermi('iot:network:edit')")
    @Log(title = "停止组件", businessType = BusinessType.UPDATE)
    @DeleteMapping(value = "/shutdown/{id}")
    public AjaxResult shutdownNetwork(@PathVariable("id") Long id) {
        return toAjax(iotNetworkConfigService.shutdownNetworkById(id));
    }

    /**
     * 删除组件
     */
    @PreAuthorize("@ss.hasPermi('iot:network:remove')")
    @Log(title = "组件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        logger.info("删除组件信息编号如下：" + ids);
        return toAjax(iotNetworkConfigService.delIotNetworkConfig(ids));
    }

    /**
     * 导出网络组件列表
     */
    @PreAuthorize("@ss.hasPermi('iot:network:export')")
    @Log(title = "组件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotNetworkConfig iotNetworkConfig) {
        logger.info("导出网络组件开始...");
        logger.info("参数如下：" + iotNetworkConfig);
        List<IotNetworkConfig> list = iotNetworkConfigService.selectIotNetworkConfigList(iotNetworkConfig);
        logger.info("查询结果如下：", list);
        ExcelUtil<IotNetworkConfig> util = new ExcelUtil<>(IotNetworkConfig.class);
        util.exportExcel(response, list, "网络组件数据");
    }

    /**
     * 获取支持的网络组件类型
     * @return
     */
    @GetMapping("/supports")
    public AjaxResult getSupports() {

        return AjaxResult.success(networkManager
                .getProviders()
                .stream()
                .map(NetworkProvider::getType)
                .map(NetworkTypeInfo::of).toArray());

    }
}
